package com.core.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.DAOException;
import com.core.dao.DAOFactory;
import com.core.entities.State;

public class StateSrv {
	
	private Connection conn;

	public StateSrv(Connection conn) {
		this.conn = conn;
	}
	
	public List<String> getStateNames(){
		List<String> statesNames = new ArrayList<String>();
		try{
			for(State state : DAOFactory.getInstance().getStateDAO(conn).selectAll())
				statesNames.add(state.getName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return statesNames;
	}
	
	public List<String> getStateByCountry(String countryName){
		List<String> statesNames = new ArrayList<String>();
		try{
			int countryId = DAOFactory.getInstance().getCountryDAO(conn).getByName(countryName).get(0).getId();
			for(State state : DAOFactory.getInstance().getStateDAO(conn).getByCountryId(countryId))
				statesNames.add(state.getName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return statesNames;
	}
	
	public int getStatePK(String stateName){
		try{
			return DAOFactory.getInstance().getStateDAO(conn).getByName(stateName).get(0).getId();
		}catch(DAOException e){
			e.printStackTrace();
		}
		return -1;
	}
}
